import com.sun.source.tree.Tree;

public class BinarySearchTree {
    class  TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    public boolean search(TreeNode root,int val) {
        TreeNode cur = root;
        while(cur != null) {
            if(cur.val == val) {
                return true;
            } else if(cur.val > val) {
                cur = cur.left;
            } else {
                cur = cur.right;
            }
        }
        return false;
    }

    public void insert(TreeNode root,int val) {
        TreeNode cur = root;
        TreeNode node = new TreeNode(val);
        TreeNode parent = root;
        while(cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else {
                return;
            }
        }
        if(parent.val < val) {
            parent.right = node;
        }else {
            parent.left = node;
        }
    }
}
